package mysql

import (
	"gorm.io/gorm"
	"manuscriptservice/manuscriptservicerpc/manuscriptservicerpc"
)

type Manuscript struct {
	gorm.Model
	Title   string `gorm:"type:varchar(30);comment:稿件名"`
	Content string `gorm:"type:varchar(50);comment:稿件内容"`
	TyperId int    `gorm:"typer:int;comment:类型ID"`
}

func (m *Manuscript) ManuscriptAdd(db *gorm.DB) error {
	return db.Debug().Create(&m).Error
}

func (m *Manuscript) FindTitle(db *gorm.DB, title string) error {
	return db.Debug().Where("manuscripts.title=?", title).First(m).Error
}
func ListManuscript(db *gorm.DB) ([]*manuscriptservicerpc.ManuscriptList, error) {
	//SELECT
	//  manuscripts.title,
	//  manuscripts.content,
	//  typers.typer_name
	//FROM
	//  manuscripts
	//  LEFT JOIN typers ON manuscripts.typer_id = typers.id
	var list []*manuscriptservicerpc.ManuscriptList
	err2 := db.Debug().Model(&Manuscript{}).Select(
		"manuscripts.title",
		"manuscripts.content",
		"typers.typer_name as type_name",
	).
		Joins("LEFT JOIN typers ON manuscripts.typer_id = typers.id").
		Find(&list).Error
	return list, err2
}
